package com.egao.common.module.AI.service.Impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.egao.common.core.web.JsonResult;
import com.egao.common.manage.CommonRefService;
import com.egao.common.module.AI.entity.StudentVector;
import com.egao.common.module.AI.entity.StudentVo;
import com.egao.common.module.AI.service.IAiService;
import com.egao.common.module.company.entity.Company;
import com.egao.common.module.company.mapper.CompanyMapper;
import com.egao.common.module.student.entity.Student;
import com.egao.common.module.student.entity.StudentInfo;
import com.egao.common.module.student.mapper.StudentInfoMapper;
import com.egao.common.module.student.mapper.StudentMapper;
import com.egao.common.module.system.constants.DictConstants;
import com.egao.common.module.system.entity.Major;
import com.egao.common.module.system.entity.User;
import com.egao.common.module.system.mapper.CollegeMapper;
import com.egao.common.module.system.mapper.MajorMapper;
import com.egao.common.module.system.mapper.UserMapper;
import com.egao.common.module.system.service.impl.DictServiceImpl;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ResourceLoader;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 *  服务实现类
 *
 * @author ikun
 */
@Service
public class AiServiceImpl  implements IAiService {

    private DictServiceImpl dictService;
    private StudentMapper studentMapper;
    private CompanyMapper companyMapper;
    private UserMapper userMapper;
    private CommonRefService refService;
    private CollegeMapper collegeMapper;
    private MajorMapper majorMapper;
    private StudentInfoMapper studentInfoMapper;
    @Autowired
    public AiServiceImpl(DictServiceImpl dictService, ResourceLoader resourceLoader, StudentMapper studentMapper, CompanyMapper companyMapper, UserMapper userMapper, CommonRefService refService, CollegeMapper collegeMapper, MajorMapper majorMapper, StudentInfoMapper studentInfoMapper) {
        this.dictService = dictService;
        this.studentMapper = studentMapper;
        this.companyMapper = companyMapper;
        this.userMapper = userMapper;
        this.refService = refService;
        this.collegeMapper = collegeMapper;
        this.majorMapper = majorMapper;
        this.studentInfoMapper = studentInfoMapper;
    }
    @Override
    public JsonResult ai() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        User curUser = (User) authentication.getPrincipal();
        Student student = studentMapper.selectOne(new QueryWrapper<Student>().eq("user_id", curUser.getUserId()));
        StudentInfo studentInfo=studentInfoMapper.selectOne(new QueryWrapper<StudentInfo>().eq("user_id", curUser.getUserId()));
        Major major = majorMapper.selectOne(new QueryWrapper<Major>().eq("code", student.getMajorCode()));
        StudentVo vo=new StudentVo();
        BeanUtils.copyProperties(student,vo);
        BeanUtils.copyProperties(studentInfo,vo);
        vo.setYxdq(dictService.cacheLabel("syszd", studentInfo.getYxdq()));//婚姻状况，属附加
        vo.setMajor(major.getName());
        vo.setYxdwxz(getYxdwxzValue(studentInfo.getYxdwxz()));//处理中文信息 意向
        vo.setYxdwhy(getYxdwhyValue(studentInfo.getYxdwhy()));
        StudentVector studentVector=new StudentVector();
        studentVector.setMajorIndex(callPythonScriptMajor(vo));
        studentVector.setAbilityIndex1(callPythonScriptAbilityZyzshjndzw(vo));//六个打分函数
        studentVector.setAbilityIndex2(callPythonScriptAbilityGtbdhxznl(vo));
        studentVector.setAbilityIndex3(callPythonScriptAbilityTdxznl(vo));
        studentVector.setAbilityIndex4(callPythonScriptAbilityCxnl(vo));
        studentVector.setAbilityIndex5(callPythonScriptAbilityKynl(vo));
        studentVector.setAbilityIndex6(callPythonScriptAbilitySxjl(vo));
        studentVector.setYxdwxz(vo.getYxdwxz());
        studentVector.setYxdwhy(vo.getYxdwhy());
        studentVector.setYxdqS(callPythonAreaS(vo));
        studentVector.setYxdqC(callPythonAreaC(vo));
        Integer bestID= Last(studentVector);
        Company company = companyMapper.selectOne(new QueryWrapper<Company>().eq("user_id", bestID));
        return JsonResult.ok().setData(company);
    }
    private String callPythonScriptMajor(StudentVo vo) {
        try {
            // 获取 vo.getAhtc() 的值
            String major = null;
            if (vo.getMajor()!=null) {
                major = vo.getMajor();
            }
            // 构建 Python 脚本路径和命令
            String pythonScriptPath = "D:\\desktop\\server\\src\\main\\java\\com\\egao\\common\\module\\AI\\model\\major.py";
            List<String> command = new ArrayList<>();
            command.add("python");
            command.add(pythonScriptPath);
            command.add(major); // 直接传递 ahtc 的值

            // 使用 ProcessBuilder
            ProcessBuilder processBuilder = new ProcessBuilder(command);
            Process process = processBuilder.start();

            // 读取 Python 脚本的标准输出
            StringBuilder output = new StringBuilder();
            try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream(), "UTF-8"))) {
                String line;
                while ((line = reader.readLine()) != null) {
                    output.append(line).append("\n");
                }
            }

            // 读取 Python 脚本的标准错误输出
            StringBuilder errorOutput = new StringBuilder();
            try (BufferedReader errorReader = new BufferedReader(new InputStreamReader(process.getErrorStream(), "UTF-8"))) {
                String errorLine;
                while ((errorLine = errorReader.readLine()) != null) {
                    errorOutput.append(errorLine).append("\n");
                }
            }

            // 等待 Python 脚本执行完成
            int exitCode = process.waitFor();
            if (exitCode != 0) {
                throw new RuntimeException("Python script exited with code " + exitCode + ". Error output: " + errorOutput.toString());
            }

            return output.toString().trim();
        } catch (Exception e) {
            e.printStackTrace();
            return "Error processing request";
        }
    }
    public static void addParameterIfNotNull(List<String> commandList, String parameter, String defaultValue) {
        if (parameter != null && !parameter.isEmpty()) {
            commandList.add(parameter);
        } else {
            commandList.add(defaultValue);
        }
    }
    private float callPythonScriptAbilityZyzshjndzw(StudentVo vo) {
        try {
            String grjn = vo.getGrjn();
            String hjjfbzpqk = vo.getHjjfbzpqk();
            String zykyhdhxshd = vo.getZykyhdhxshd();

            // 构建 Python 脚本路径和命令
            String pythonScriptPath = "D:\\desktop\\server\\src\\main\\java\\com\\egao\\common\\module\\AI\\model\\evaluateZyzshjndzw.py";
            List<String> command = new ArrayList<>();
            command.add("python");
            command.add(pythonScriptPath);
            addParameterIfNotNull(command, grjn, "无");
            addParameterIfNotNull(command, hjjfbzpqk, "无");
            addParameterIfNotNull(command, zykyhdhxshd, "无");

            // 使用 ProcessBuilder
            ProcessBuilder processBuilder = new ProcessBuilder(command);
            Process process = processBuilder.start();

            // 读取 Python 脚本的标准输出
            StringBuilder output = new StringBuilder();
            try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream(), "UTF-8"))) {
                String line;
                while ((line = reader.readLine()) != null) {
                    output.append(line).append("\n");
                }
            }

            // 读取 Python 脚本的标准错误输出
            StringBuilder errorOutput = new StringBuilder();
            try (BufferedReader errorReader = new BufferedReader(new InputStreamReader(process.getErrorStream(), "UTF-8"))) {
                String errorLine;
                while ((errorLine = errorReader.readLine()) != null) {
                    errorOutput.append(errorLine).append("\n");
                }
            }

            // 等待 Python 脚本执行完成
            int exitCode = process.waitFor();
            if (exitCode != 0) {
                throw new RuntimeException("Python script exited with code " + exitCode + ". Error output: " + errorOutput.toString());
            }

            // 解析输出为浮点数
            float result = Float.parseFloat(output.toString().trim());
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return -1.0f; // 返回一个错误值，例如 -1.0
        }
    }
    private float callPythonScriptAbilityGtbdhxznl(StudentVo vo) {
        try {
            String grjn = vo.getGrjn();
            // 构建 Python 脚本路径和命令
            String pythonScriptPath = "D:\\desktop\\server\\src\\main\\java\\com\\egao\\common\\module\\AI\\model\\evaluateGtbdhxznl.py";
            List<String> command = new ArrayList<>();
            command.add("python");
            command.add(pythonScriptPath);
            addParameterIfNotNull(command, grjn, "无");

            // 使用 ProcessBuilder
            ProcessBuilder processBuilder = new ProcessBuilder(command);
            Process process = processBuilder.start();

            // 读取 Python 脚本的标准输出
            StringBuilder output = new StringBuilder();
            try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream(), "UTF-8"))) {
                String line;
                while ((line = reader.readLine()) != null) {
                    output.append(line).append("\n");
                }
            }

            // 读取 Python 脚本的标准错误输出
            StringBuilder errorOutput = new StringBuilder();
            try (BufferedReader errorReader = new BufferedReader(new InputStreamReader(process.getErrorStream(), "UTF-8"))) {
                String errorLine;
                while ((errorLine = errorReader.readLine()) != null) {
                    errorOutput.append(errorLine).append("\n");
                }
            }

            // 等待 Python 脚本执行完成
            int exitCode = process.waitFor();
            if (exitCode != 0) {
                throw new RuntimeException("Python script exited with code " + exitCode + ". Error output: " + errorOutput.toString());
            }

            // 解析输出为浮点数
            float result = Float.parseFloat(output.toString().trim());
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return -1.0f; // 返回一个错误值，例如 -1.0
        }
    }
    private float callPythonScriptAbilityTdxznl(StudentVo vo) {
        try {
            String grjn = vo.getGrjn();
            // 构建 Python 脚本路径和命令
            String pythonScriptPath = "D:\\desktop\\server\\src\\main\\java\\com\\egao\\common\\module\\AI\\model\\evaluateTdxznl.py";
            List<String> command = new ArrayList<>();
            command.add("python");
            command.add(pythonScriptPath);
            addParameterIfNotNull(command, grjn, "无");

            // 使用 ProcessBuilder
            ProcessBuilder processBuilder = new ProcessBuilder(command);
            Process process = processBuilder.start();

            // 读取 Python 脚本的标准输出
            StringBuilder output = new StringBuilder();
            try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream(), "UTF-8"))) {
                String line;
                while ((line = reader.readLine()) != null) {
                    output.append(line).append("\n");
                }
            }

            // 读取 Python 脚本的标准错误输出
            StringBuilder errorOutput = new StringBuilder();
            try (BufferedReader errorReader = new BufferedReader(new InputStreamReader(process.getErrorStream(), "UTF-8"))) {
                String errorLine;
                while ((errorLine = errorReader.readLine()) != null) {
                    errorOutput.append(errorLine).append("\n");
                }
            }

            // 等待 Python 脚本执行完成
            int exitCode = process.waitFor();
            if (exitCode != 0) {
                throw new RuntimeException("Python script exited with code " + exitCode + ". Error output: " + errorOutput.toString());
            }

            // 解析输出为浮点数
            float result = Float.parseFloat(output.toString().trim());
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return -1.0f; // 返回一个错误值，例如 -1.0
        }
    }
    private float callPythonScriptAbilityCxnl(StudentVo vo) {
        try {
            String hjjfbzpqk = vo.getHjjfbzpqk();
            String zykyhdhxshd = vo.getZykyhdhxshd();
            // 构建 Python 脚本路径和命令
            String pythonScriptPath = "D:\\desktop\\server\\src\\main\\java\\com\\egao\\common\\module\\AI\\model\\evaluateCxnl.py";
            List<String> command = new ArrayList<>();
            command.add("python");
            command.add(pythonScriptPath);
            addParameterIfNotNull(command, hjjfbzpqk, "无");
            addParameterIfNotNull(command, zykyhdhxshd, "无");

            // 使用 ProcessBuilder
            ProcessBuilder processBuilder = new ProcessBuilder(command);
            Process process = processBuilder.start();

            // 读取 Python 脚本的标准输出
            StringBuilder output = new StringBuilder();
            try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream(), "UTF-8"))) {
                String line;
                while ((line = reader.readLine()) != null) {
                    output.append(line).append("\n");
                }
            }

            // 读取 Python 脚本的标准错误输出
            StringBuilder errorOutput = new StringBuilder();
            try (BufferedReader errorReader = new BufferedReader(new InputStreamReader(process.getErrorStream(), "UTF-8"))) {
                String errorLine;
                while ((errorLine = errorReader.readLine()) != null) {
                    errorOutput.append(errorLine).append("\n");
                }
            }

            // 等待 Python 脚本执行完成
            int exitCode = process.waitFor();
            if (exitCode != 0) {
                throw new RuntimeException("Python script exited with code " + exitCode + ". Error output: " + errorOutput.toString());
            }

            // 解析输出为浮点数
            float result = Float.parseFloat(output.toString().trim());
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return -1.0f; // 返回一个错误值，例如 -1.0
        }
    }
    private float callPythonScriptAbilityKynl(StudentVo vo) {
        try {
            String grjn = vo.getGrjn();
            // 构建 Python 脚本路径和命令
            String pythonScriptPath = "D:\\desktop\\server\\src\\main\\java\\com\\egao\\common\\module\\AI\\model\\evaluateKynl.py";
            List<String> command = new ArrayList<>();
            command.add("python");
            command.add(pythonScriptPath);
            addParameterIfNotNull(command, grjn, "无");

            // 使用 ProcessBuilder
            ProcessBuilder processBuilder = new ProcessBuilder(command);
            Process process = processBuilder.start();

            // 读取 Python 脚本的标准输出
            StringBuilder output = new StringBuilder();
            try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream(), "UTF-8"))) {
                String line;
                while ((line = reader.readLine()) != null) {
                    output.append(line).append("\n");
                }
            }

            // 读取 Python 脚本的标准错误输出
            StringBuilder errorOutput = new StringBuilder();
            try (BufferedReader errorReader = new BufferedReader(new InputStreamReader(process.getErrorStream(), "UTF-8"))) {
                String errorLine;
                while ((errorLine = errorReader.readLine()) != null) {
                    errorOutput.append(errorLine).append("\n");
                }
            }

            // 等待 Python 脚本执行完成
            int exitCode = process.waitFor();
            if (exitCode != 0) {
                throw new RuntimeException("Python script exited with code " + exitCode + ". Error output: " + errorOutput.toString());
            }

            // 解析输出为浮点数
            float result = Float.parseFloat(output.toString().trim());
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return -1.0f; // 返回一个错误值，例如 -1.0
        }
    }
    private float callPythonScriptAbilitySxjl(StudentVo vo) {
        try {
            String shsjjljrzqk = vo.getShsjjljrzqk();
            // 构建 Python 脚本路径和命令
            String pythonScriptPath = "D:\\desktop\\server\\src\\main\\java\\com\\egao\\common\\module\\AI\\model\\evaluateSxjl.py";
            List<String> command = new ArrayList<>();
            command.add("python");
            command.add(pythonScriptPath);
            addParameterIfNotNull(command, shsjjljrzqk, "无");

            // 使用 ProcessBuilder
            ProcessBuilder processBuilder = new ProcessBuilder(command);
            Process process = processBuilder.start();

            // 读取 Python 脚本的标准输出
            StringBuilder output = new StringBuilder();
            try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream(), "UTF-8"))) {
                String line;
                while ((line = reader.readLine()) != null) {
                    output.append(line).append("\n");
                }
            }

            // 读取 Python 脚本的标准错误输出
            StringBuilder errorOutput = new StringBuilder();
            try (BufferedReader errorReader = new BufferedReader(new InputStreamReader(process.getErrorStream(), "UTF-8"))) {
                String errorLine;
                while ((errorLine = errorReader.readLine()) != null) {
                    errorOutput.append(errorLine).append("\n");
                }
            }

            // 等待 Python 脚本执行完成
            int exitCode = process.waitFor();
            if (exitCode != 0) {
                throw new RuntimeException("Python script exited with code " + exitCode + ". Error output: " + errorOutput.toString());
            }

            // 解析输出为浮点数
            float result = Float.parseFloat(output.toString().trim());
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return -1.0f; // 返回一个错误值，例如 -1.0
        }
    }
    private String callPythonAreaS(StudentVo vo) {
        try {
            String yxdq = vo.getYxdq();
            // 构建 Python 脚本路径和命令
            String pythonScriptPath = "D:\\desktop\\server\\src\\main\\java\\com\\egao\\common\\module\\AI\\model\\province.py";
            List<String> command = new ArrayList<>();
            command.add("python");
            command.add(pythonScriptPath);
            // 使用封装方法添加参数
            addParameterIfNotNull(command, yxdq, "无");

            // 使用 ProcessBuilder
            ProcessBuilder processBuilder = new ProcessBuilder(command);
            Process process = processBuilder.start();

            // 读取 Python 脚本的标准输出
            StringBuilder output = new StringBuilder();
            try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream(), "UTF-8"))) {
                String line;
                while ((line = reader.readLine()) != null) {
                    output.append(line).append("\n");
                }
            }

            // 读取 Python 脚本的标准错误输出
            StringBuilder errorOutput = new StringBuilder();
            try (BufferedReader errorReader = new BufferedReader(new InputStreamReader(process.getErrorStream(), "UTF-8"))) {
                String errorLine;
                while ((errorLine = errorReader.readLine()) != null) {
                    errorOutput.append(errorLine).append("\n");
                }
            }

            // 等待 Python 脚本执行完成
            int exitCode = process.waitFor();
            if (exitCode != 0) {
                throw new RuntimeException("Python script exited with code " + exitCode + ". Error output: " + errorOutput.toString());
            }

            return output.toString().trim();
        } catch (Exception e) {
            e.printStackTrace();
            return "Error processing request";
        }
    }
    private String callPythonAreaC(StudentVo vo) {
        try {
            String yxdq = vo.getYxdq();

            // 构建 Python 脚本路径和命令
            String pythonScriptPath = "D:\\desktop\\server\\src\\main\\java\\com\\egao\\common\\module\\AI\\model\\city.py";
            List<String> command = new ArrayList<>();
            command.add("python");
            command.add(pythonScriptPath);
            // 使用封装方法添加参数
            addParameterIfNotNull(command, yxdq, "无");

            // 使用 ProcessBuilder
            ProcessBuilder processBuilder = new ProcessBuilder(command);
            Process process = processBuilder.start();

            // 读取 Python 脚本的标准输出
            StringBuilder output = new StringBuilder();
            try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream(), "UTF-8"))) {
                String line;
                while ((line = reader.readLine()) != null) {
                    output.append(line).append("\n");
                }
            }

            // 读取 Python 脚本的标准错误输出
            StringBuilder errorOutput = new StringBuilder();
            try (BufferedReader errorReader = new BufferedReader(new InputStreamReader(process.getErrorStream(), "UTF-8"))) {
                String errorLine;
                while ((errorLine = errorReader.readLine()) != null) {
                    errorOutput.append(errorLine).append("\n");
                }
            }

            // 等待 Python 脚本执行完成
            int exitCode = process.waitFor();
            if (exitCode != 0) {
                throw new RuntimeException("Python script exited with code " + exitCode + ". Error output: " + errorOutput.toString());
            }

            return output.toString().trim();
        } catch (Exception e) {
            e.printStackTrace();
            return "Error processing request";
        }
    }
    public static String wrapNumbersToString(int x, int y) {
        return String.format("[%s, %s]", x, y);
    }
    // 包装 AreaVector 的方法
    private String wrapAreaVector(String area) {
        if (area == null || area.isEmpty()) {
            return "无";
        }
        return String.format("'[\"%s\"]'", area);
    }
    // 为字符串添加双引号的方法
    private String wrapWithQuotes(String input) {
        if (input == null || input.isEmpty()) {
            return "无";
        }
        return "\"" + input + "\"";
    }
    private String Vector(StudentVector studentVector){
        // 将这些参数放入一个数组中
        float[] vector = {studentVector.getAbilityIndex1(), studentVector.getAbilityIndex2(), studentVector.getAbilityIndex3(),
                studentVector.getAbilityIndex4(), studentVector.getAbilityIndex5(), studentVector.getAbilityIndex6()};

        return vectorToString(vector);
    }
    public static String vectorToString(float[] vector) {
        if (vector == null || vector.length != 6) {
            throw new IllegalArgumentException("Vector must contain exactly 6 elements.");
        }
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < vector.length; i++) {
            sb.append(vector[i]);
            if (i < vector.length - 1) {
                sb.append(",");
            }
        }
        return sb.toString();
    }
    private Integer Last(StudentVector vector) {
        try {
            String MajorVector=vector.getMajorIndex();
            String AbilityVector=Vector(vector);
            String NatureVector=wrapNumbersToString(vector.getYxdwxz(), vector.getYxdwhy());
            String AreaSVector=vector.getYxdqS();
            String AreaCVector=vector.getYxdqC();
            // 为 AreaSVector 和 AreaCVector 套壳子
            AreaSVector = wrapAreaVector(AreaSVector);
            AreaCVector = wrapAreaVector(AreaCVector);
            // 构建 Python 脚本路径和命令
            String pythonScriptPath = "D:\\desktop\\server\\src\\main\\java\\com\\egao\\common\\module\\AI\\model\\model.py";
            List<String> command = new ArrayList<>();
            command.add("python");
            command.add(pythonScriptPath);
            // 使用封装方法添加参数
            addParameterIfNotNull(command, wrapWithQuotes(MajorVector), "无");
            addParameterIfNotNull(command, wrapWithQuotes(AbilityVector), "无");
            addParameterIfNotNull(command, wrapWithQuotes(NatureVector), "无");
            addParameterIfNotNull(command, AreaSVector, "无");
            addParameterIfNotNull(command, AreaCVector, "无");

            // 使用 ProcessBuilder
            ProcessBuilder processBuilder = new ProcessBuilder(command);
            Process process = processBuilder.start();

            // 读取输出
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String output = reader.readLine();  // 读取第一行输出

            // 等待 Python 脚本执行完成
            int exitCode = process.waitFor();
            if (exitCode != 0) {
                throw new RuntimeException("Python script exited with code " + exitCode + ". Error output: ");
            }

            return parseIntegerFromOutput(output);
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }
    private static Integer parseIntegerFromOutput(String output) {
        if (output == null || output.isEmpty()) {
            throw new IllegalArgumentException("Output is null or empty");
        }

        // 去掉首尾的方括号
        output = output.trim();
        if (output.startsWith("[") && output.endsWith("]")) {
            output = output.substring(1, output.length() - 1).trim();
        }

        // 将字符串转换为整数
        return Integer.valueOf(output);
    }
    private Integer getYxdwxzValue(String yxdwxz) {
        switch (yxdwxz) {
            case "10": // 拟升学、出国
                return 0;
            case "20": // 暂不就业
                return 0;
            case "0": // 不限
                return 1;
            case "DA": // 部队
                return 2;
            case "AC": // 党政机关
                return 3;
            case "AD": // 社会团体 -> 事业单位
                return 4;
            case "AE": // 事业单位
                return 5;
            case "CA": // 内资企业 -> 国有企业
                return 6;
            case "CB": // 港澳台投资企业
                return 7;
            case "CC": // 国外投资企业 -> 外商投资企业
                return 8;
            case "FA": // 其他
                return 9;
            default: // 其他
                return 99;
        }
    }
    private Integer getYxdwhyValue(String yxdwhy) {
        switch (yxdwhy) {
            case "A": // 农、林、牧、渔业
                return 2;
            case "B": // 采矿业
                return 3;
            case "C": // 制造业
                return 4;
            case "D": // 电力、热力、燃气及水生产和供应业
                return 5;
            case "E": // 建筑业
                return 6;
            case "H": // 批发和零售业
                return 9;
            case "F": // 交通运输、仓储和邮政业
                return 7;
            case "I": // 住宿和餐饮业
                return 10;
            case "G": // 信息传输、软件和信息技术服务业
            case "G60":
            case "G61":
            case "G62":
                return 8;
            case "J": // 金融业
                return 11;
            case "K": // 房地产业
                return 12;
            case "L": // 租赁和商务服务业
                return 13;
            case "M": // 科学研究和技术服务业
                return 14;
            case "N": // 水利、环境和公共设施管理业
                return 15;
            case "O": // 居民服务、修理和其他服务业
                return 16;
            case "P": // 教育
                return 17;
            case "Q": // 卫生和社会工作
                return 18;
            case "R": // 文化、体育和娱乐业
                return 19;
            case "S": // 公共管理、社会保障和社会组织
            case "S93":
            case "S94":
            case "S95":
            case "S96":
            case "S97":
                return 20;
            case "T": // 国际组织
                return 21;
            default: // 其他
                return 99;
        }
    }
}